// app/service/user.js
const Service = require('egg').Service;

class HouseService extends Service {
  // async find(uid) {
  //   const user = await this.ctx.db.query(
  //     'select * from user where uid = ?',
  //     uid,
  //   );
  //   return user;
  // }

  // 获取房源列表
  async getHouseList() {
    let sql = `select * from house`;
    const res = await this.ctx.app.mysql.query(sql, []);
    if (res.length > 0) {
      return {
        status: true,
        code: 200,
        message: '获取列表成功',
        data: res
      }
    } else {
      return {
        status: false,
        code: 403,
        message: '获取列表失败',
        data: []
      }
    }
  }
  //删除
  async DeleteHouse(params) {
    let { id } = params;
    let sql = `delete from house where id = ?`
    let res = await this.ctx.app.mysql.query(sql, [id])
    if (res.length > 0) {
      return {
        status: true,
        code: 200,
        message: '删除成功',
        data: res
      }
    } else {
      return {
        status: false,
        code: 403,
        message: '删除失败',
        data: []
      }
    }

  }

  //新增
  async addHouse(params) {
    let { id, house_name, house_address, house_price, house_date, people } = params;
    let sql = `insert into house (id,house_name,house_address,house_price,house_date,people) values 
    (?,?,?,?,?,?)`
    const res = await this.ctx.app.mysql.query(sql, [
      id, house_name, house_address, house_price, house_date, people
    ]);
    if (res.length > 0) {
      return {
        status: true,
        code: 200,
        message: '新增房源成功',
        data: res
      }
    } else {
      return {
        status: false,
        code: 403,
        message: '新增房源失败',
        data: []
      }
    }
  }

  //编辑
  async editHouse(params) {
    let { id,house_name,house_address,house_price,house_date,people } = params;
    let sql = `update house set house_name=?,house_address=?,house_price=?,house_date=?,people=? where id = ?`;
    const res = await this.ctx.app.mysql.query(sql, [
      house_name,
      house_address,
      house_price,
      house_date,
      people,
      id
    ])
    if (res.length > 0) {
      return {
        status: true,
        code: 200,
        message: '更新房源成功',
        data: res
      }
    } else {
      return {
        status: false,
        code: 403,
        message: '更新房源失败',
        data: []
      }
    }
  }

}

module.exports = HouseService;